#include <iostream>
#include <cstdio>

using namespace std;

char circuit[105][105];
int h;
int x, y;

enum DIR
{
    LEFT,
    UP,
    RIGHT,
    DOWN
};

struct Node
{
    int type; // 0-25表示对应输入状态的序号(A-Z); -2 and; -3 or; -4 not.
    int left_child;
    int right_child;
};

Node tree[400];
int tree_len;

char input[27];

bool ReadCircuit()
{
    memset(circuit, 0, sizeof(circuit));
    if (!gets(circuit[0]))
        return false;
    h = 1;
    while (gets(circuit[h++]))
    {
        if (circuit[h - 1][0] == '*')
        {
            --h;
            break;
        }
    }
    return true;
}

void FindOutput()
{
    for (int i = 0; i < h; ++i)
    {
        for (int j = 0; j < 100; ++j)
        {
            if (circuit[i][j] == '\0')
                break;
            if (circuit[i][j] == '?')
            {
                x = i;
                y = j;
                return;
            }
        }
    }
}

void ConstructTree(int x, int y, int node_id, DIR dir)
{
    switch (circuit[x][y])
    {
    case '?':
        if (circuit[x][y - 1] == '-')
            ConstructTree(x, y - 2, node_id, LEFT);
        else if (circuit[x][y + 1] == '-')
            ConstructTree(x, y + 2, node_id, RIGHT);
        else if (circuit[x - 1][y] == '|')
            ConstructTree(x - 2, y, node_id, UP);
        else if (circuit[x + 1][y] == '|')
            ConstructTree(x + 2, y, node_id, DOWN);
        else
            ConstructTree(x, y - 1, node_id, LEFT);
        break;
    case ')':
        tree[node_id].type = -2; // and
        tree[node_id].left_child = ++tree_len;
        tree[node_id].right_child = ++tree_len;
        ConstructTree(x - 1, y - 3, tree[node_id].left_child, LEFT);
        ConstructTree(x + 1, y - 3, tree[node_id].right_child, LEFT);
        break;
    case '>':
        tree[node_id].type = -3; // or
        tree[node_id].left_child = ++tree_len;
        tree[node_id].right_child = ++tree_len;
        ConstructTree(x - 1, y - 3, tree[node_id].left_child, LEFT);
        ConstructTree(x + 1, y - 3, tree[node_id].right_child, LEFT);
        break;
    case 'o':
        tree[node_id].type = -4; // not
        tree[node_id].left_child = ++tree_len;
        tree[node_id].right_child = -1;
        if (circuit[x][y - 1] == ')' || circuit[x][y - 1] == '>')
            ConstructTree(x, y - 1, tree[node_id].left_child, LEFT);
        else if (circuit[x][y - 1] == '-' && dir != RIGHT)
            ConstructTree(x, y - 2, tree[node_id].left_child, LEFT);
        else if (circuit[x][y + 1] == '-' && dir != LEFT)
            ConstructTree(x, y - 2, tree[node_id].left_child, RIGHT);
        else if (circuit[x - 1][y] == '|' && dir != DOWN)
            ConstructTree(x - 2, y, tree[node_id].left_child, UP);
        else
            ConstructTree(x + 2, y, tree[node_id].left_child, DOWN);
        break;
    case '+':
        if (dir == UP || dir == DOWN)
        {
            if (circuit[x][y - 1] == '-')
                ConstructTree(x, y - 2, node_id, LEFT);
            else
                ConstructTree(x, y + 2, node_id, RIGHT);
        }
        else
        {
            if (circuit[x - 1][y] == '|')
                ConstructTree(x - 2, y, node_id, UP);
            else
                ConstructTree(x + 2, y, node_id, DOWN);
        }
        break;
    case '-':
        if (dir == LEFT)
            ConstructTree(x, y - 1, node_id, LEFT);
        else
            ConstructTree(x, y + 1, node_id, RIGHT);
        break;
    case '|':
        if (dir == UP)
            ConstructTree(x - 1, y, node_id, UP);
        else
            ConstructTree(x + 1, y, node_id, DOWN);
        break;
    default:
        tree[node_id].left_child = -1;
        tree[node_id].right_child = -1;
        tree[node_id].type = circuit[x][y] - 'A';
    }
}

bool GetInput()
{
    scanf("%s", input);
    if (input[0] == '*')
        return false;
    return true;
}

int GetOutput(int i)
{
    switch (tree[i].type)
    {
    case -2: // and
        return GetOutput(tree[i].left_child) && GetOutput(tree[i].right_child);
    case -3: // or
        return GetOutput(tree[i].left_child) || GetOutput(tree[i].right_child);
    case -4: // not
        return !GetOutput(tree[i].left_child);
    default:
        return input[tree[i].type] - '0';
    }
}

int main()
{
    while (ReadCircuit())
    {
        FindOutput();
        tree_len = 0;
        ConstructTree(x, y, 0, LEFT);
        while (GetInput())
        {
            printf("%d\n", GetOutput(0));
        }
        printf("\n");
    }
    return 0;
}